package chapt02.bounce;
// Bounce.java
// Demonstrates a simple animated rectangle program
// OpenGL SuperBible, 3rd Edition
// Richard S. Wright Jr.
// rwright@starstonesoftware.com

import javax.media.opengl.GL;
import javax.media.opengl.GL2;

public class Bounce
{
	// Initial square position and size
	static float x = 0.0f;
	static float y = 0.0f;
	static float rsize = 25;
	
	// Step size in x and y directions
	// (number of pixels to move each time)
	static float xstep = 1.0f;
	static float ystep = 1.0f;
	
	// Keep track of windows changing width and height
	static float windowWidth;
	static float windowHeight;
	
	// Setup the rendering state
	// Called by GLUT library when the window has changed size
	protected static void setup(GL2 gl2, int w, int h)
	{	
		gl2.glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
		
		float aspectRatio;
		
		// Prevent a divide by zero
		if(h == 0)
			h = 1;
		
		// Set Viewport to window dimensions
		gl2.glViewport(0, 0, w, h);
		
		// Reset coordinate system
		gl2.glMatrixMode(GL2.GL_PROJECTION);
		gl2.glLoadIdentity();
		
		// Establish clipping volume (left, right, bottom, top, near, far)
		aspectRatio = (float)w / (float)h;
		if(w <= h)
			gl2.glOrtho(-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
		else
			gl2.glOrtho(-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0);
		
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glLoadIdentity();
	}
	
	// Called to draw scene
	protected static void render(GL2 gl2, int w, int h)
	{
  	  // Reverse direction when you reach left or right edge
  	  if(x > windowWidth - rsize || x < -windowWidth)
  		  xstep = -xstep;
  	  
  	  // Reverse direction when you reach top or bottom edge
  	  if(y > windowHeight || y < -windowHeight + rsize)
  		  ystep = -ystep;
  	  
  	  // Actually move the square
  	  x += xstep;
  	  y += ystep;
  	  
  	  // Check bounds.  This is in case the window is made
  	  // smaller while the rectangle is bouncing and the
  	  // rectangle suddenly finds itself outside the new
  	  // clipping volume.
  	  if(x > (windowWidth - rsize + xstep))
  		  x = windowWidth - rsize - 1;
  	  else if(x < -(windowWidth + xstep))
  		  x = -windowWidth - 1;
  	  
  	  if(y > (windowHeight + ystep))
  		  y = windowHeight - 1;
  	  else if(y < -(windowHeight - rsize + ystep))
  		  y = -windowHeight + rsize - 1;

		float aspectRatio;
		
		// Prevent a divide by zero
		if(h == 0)
			h = 1;
		
		// Set Viewport to window dimensions
		gl2.glViewport(0, 0, w, h);
		
		// Reset coordinate system
		gl2.glMatrixMode(GL2.GL_PROJECTION);
		gl2.glLoadIdentity();
		
		// Establish clipping volume (left, right, bottom, top, near, far)
		aspectRatio = (float)w / (float)h;
		if(w <= h)
		{
			windowWidth = 100;
			windowHeight = 100 / aspectRatio;
			gl2.glOrtho(-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0);
		}
		else
		{
			windowWidth = 100 * aspectRatio;
			windowHeight = 100;
			gl2.glOrtho(-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0);
		}
		
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glLoadIdentity();
		
		// Clear the window with current clearing color
		gl2.glClear(GL.GL_COLOR_BUFFER_BIT);
		
		// Set current drawing color to red
		//              R     G     B
		gl2.glColor3f(1.0f, 0.0f, 0.0f);
		
		// Draw a filled rectangle with current color
		gl2.glRectf(x, y, x + rsize, y - rsize);
		
		// Flush drawing commands and swap
		gl2.glFlush();
	}
}